const App = getApp()
//计数器
var interval = null;
//值越大旋转时间越长  即旋转速度
var intime = 100;
Page({
  data: {
    images: [],
    btnconfirm: '/img/dianjichoujiang.png',
    color: [1, 1, 1, 1, 1, 1, 1, 1],
    clickLuck: 'clickLuck',
    luckPosition: '',
    hidden: false,
    msgList: [],
    prize:'',
    nodata:false
  },
  onLoad: function (options) {
    let that = this
    that.info()
  },
  onShow: function () {

  },
  info() {
    let that = this
    App.api.$http({
      url: 'draw.Active/detail',
      method: 'Post',
      data: {}
    }, {}).then(res => {
      if(!res.data){
        that.setData({
          nodata:true
        })
        return
      }
      if(res.data.active.playNum.num==0 && 0){
        that.setData({
          btnconfirm: '/img/dianjichoujiangd.png',
          clickLuck: ''
        })
      }
      that.setData({
        ...res.data.active,
        images: res.data.active.prizeList.data,
        msgList:res.data.active.recordList.data
      })
    })
  },
  // 点击抽奖
  clickLuck() {
    let that = this
    // 设置抽奖按钮不可点
    that.setData({
      btnconfirm: '/img/dianjichoujiangd.png',
      clickLuck: ''
    })
    clearInterval(interval)
    

    App.api.$http({
      url: 'draw.Active/draw',
      method: 'POST',
      data: {}
    }, {}).then(res => {
      if(!res){
        that.setData({
          btnconfirm: '/img/dianjichoujiang.png',
          clickLuck: 'clickLuck',
        })
        return;
      }
      var index = 0
      interval = setInterval(function () {
        if (index > 7) {
          index = 0;
          that.data.color[7] = 0.5
        } else if (index != 0) {
          that.data.color[index - 1] = 0.5
        }
        that.data.color[index] = 1
        that.setData({
          color: that.data.color,
        })
        index++;
      }, intime);
      let list = that.data.images
      let prizeId = res.data.prizeId
      let aa = list.findIndex((v) => {
        return v.id == prizeId
      })
      that.setData({
        luckPosition: aa,
        prize:res.data.prize
      })
      var stoptime = 2000;
      setTimeout(function () {
        that.stop(that.data.luckPosition);
      }, stoptime)
    })
   
  },
  stop(which) {
    let that = this
    clearInterval(interval)
    let current = -1;
    let color = that.data.color;
    for (var i = 0; i < color.length; i++) {
      if (color[i] == 1) {
        current = i;
      }
    }
    let index = current + 1;
    that.stopLuck(which, index, intime, 10);
  },
  stopLuck(which, index, time, splittime) {
    let that = this
    var color = that.data.color;
    setTimeout(function () {
      if (index > 7) {
        index = 0;
        color[7] = 0.5
      } else if (index != 0) {
        color[index - 1] = 0.5
      }
      color[index] = 1
      that.setData({
        color: color,
      })
      if ((time < 400 || index != which) && index != -1) {
        //越来越慢
        splittime++;
        time += splittime;
        //当前位置+1
        index++;
        that.stopLuck(which, index, time, splittime);
      } else {
        if(that.data.prize.id!=''){
          wx.showModal({
            title: '提示',
            content: '恭喜您获得'+that.data.prize.name+',可在中奖记录中查看',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: that.data.prize.name,
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
        that.setData({
          btnconfirm: '/img/dianjichoujiang.png',
          clickLuck: 'clickLuck',
        })
        that.setData({
          color: [1, 1, 1, 1, 1, 1, 1, 1]
        })
      }
    }, time)

  },
  rules(e) {
    let that = this
    that.setData({
      hidden: true
    })
  },
  good() {
    let that = this
    that.setData({
      hidden: false
    })
  },
  onHide: function () {

  },

})